package cn.zebra.dev.shop.module.shop.controller;

import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.member.module.member.entity.MemberEntity;
import cn.zebra.dev.member.module.member.service.MemberService;
import cn.zebra.dev.other.module.shopAfter.entity.ShopAfterEntity;
import cn.zebra.dev.shop.module.shop.dto.ShopDTO;
import cn.zebra.dev.shop.module.shop.entity.ShopEntity;
import cn.zebra.dev.shop.module.shop.service.ShopService;
import cn.zebra.dev.shop.module.shop.vo.ShopUpdateStateVO;
import cn.zebra.dev.shop.module.shop.vo.ShopVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(tags = "店铺信息管理")
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private MemberService memberService;

    @GetMapping("/list")
    @ApiOperation("店铺信息列表")
    public Result<PageUtils<ShopDTO>> list(
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("limit", limit);
        params.put("page", page);
        PageUtils<ShopEntity> pageUtils = shopService.findPage(params);
        PageUtils<ShopDTO> data = new PageUtils<>(pageUtils);
        List<ShopEntity> list = pageUtils.getList();
        if (list.size() > 0) {
            List<ShopDTO> dtoList = new LinkedList<>();
            List<Long> memberIds = list.stream().map(ShopEntity::getShopUserId).collect(Collectors.toList());
            List<MemberEntity> memberEntities=memberService.findByIds(memberIds);
            list.forEach(entity -> {
                ShopDTO dto = new ShopDTO();
                BeanUtils.copyProperties(entity, dto);
                Optional<MemberEntity> memberEntityOptional = memberEntities.stream().filter(memberEntity -> memberEntity.getId().equals(entity.getShopUserId())).findAny();
                if(memberEntityOptional.isPresent()){
                    MemberEntity memberEntity = memberEntityOptional.get();
                    dto.setShopUserName(memberEntity.getMemberName());
                }
                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }

    @PostMapping("/update/{shopId}")
    @ApiOperation("修改店铺信息")
    public Result save(@PathVariable Long shopId,@Valid  @RequestBody ShopVO vo) {
//        ValidatorUtils.validateEntity(vo);
        ShopEntity entity = new ShopEntity();
        BeanUtils.copyProperties(vo, entity);
        entity.setId(shopId);
        if (!shopService.updateById(entity)){
            return Result.success("修改失败!");
        }
        return Result.success("修改成功!");
    }

    @PostMapping("/updateStatus/{shopId}")
    @ApiOperation("冻结，解冻店铺（0，为正常；1为冻结）")
    public Result save(@PathVariable Long shopId, @Valid @RequestBody ShopUpdateStateVO vo) {
        ShopEntity entity = new ShopEntity();
        entity.setShopStatus(vo.getShopState());
        entity.setId(shopId);
        if (!shopService.updateById(entity)){
            return Result.success("操作失败!");
        }
        return Result.success("操作成功!");
    }

    @PostMapping("/save")
    @ApiOperation("新增店铺信息")
    public Result save(@Valid @RequestBody ShopVO vo) {
//        ValidatorUtils.validateEntity(vo);
        ShopEntity entity = new ShopEntity();
        entity.setCreateTime(LocalDateTime.now());
        entity.setTotalCollect(0L);
        entity.setShopStatus(0);
        BeanUtils.copyProperties(vo, entity);
        if (shopService.save(entity)) {
            return Result.success("添加成功!");
        }
        return Result.error("保存失败!");
    }


}
